# 定义一个类，每个页面的相同属性和行为
from appium import webdriver
from appium.webdriver.common.mobileby import MobileBy


class BasePage:
    # 字典
    # 1.设置终端参数项
    # 属性？ 获取
    desired_caps = {
        "platformName": "Android",
        "platformVersion": "5.1.1",
        "deviceName": "Samsung Galaxy S10",
        "appPackage": "com.tal.kaoyan",
        "appActivity": "com.tal.kaoyan.ui.activity.SplashActivity",
        "noReset": True
    }
    # 2.appium server进行启动

    # 3.发送指令给到appium server
    driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps)

    def __init__(self, driver):
        self.driver = driver

    # 行为？ 输入  点击
    # 元素定位
    def locator(self, loc):
        loc = (MobileBy.ID, "resourceid值")
        return self.driver.find_element(*loc)  # *去掉括号

    # 输入
    def input(self, loc, value):
        self.locator(loc).send_keys(value)

    # 点击
    def click(self, loc):
        self.locator(loc).click()

    # 滑动（上下左右滑动）
    def swipe(self, start_x, start_y, end_x, end_y, duration=0):
        # 获取整个屏幕尺寸
        window_size = self.driver.get_window_size()
        print("手机屏幕的尺寸", window_size)
        x = window_size["width"]
        y = window_size["height"]
        self.driver.swipe(start_x=x * start_x,
                          start_y=y * start_y,
                          end_x=x * end_x,
                          end_y=y * end_y,
                          duration=duration)
